package com.itheima.config;


import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.util.Properties;

@MapperScan("com.itheima.mapper") //扫描Mapper对象，交给Spring管理
public class MyBatisConfig {

    //配置分页插件拦截器
    @Bean
    public PageInterceptor pageInterceptor(){
        PageInterceptor pageInterceptor =new PageInterceptor();

        //分页插件配置
        Properties properties = new Properties();
        properties.setProperty("value","true");
        pageInterceptor.setProperties(properties);

        return pageInterceptor;
    }

    @Autowired
    private PageInterceptor pageInterceptor;
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.ithrima.pojo");

        //将分页插件交给mybatis
        sqlSessionFactoryBean.setPlugins(pageInterceptor);

        return sqlSessionFactoryBean;
    }
}
